package abc.database.interfaces;

import abc.model.DSInfo;
import abc.utils.StringUtil;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Data
@Component
public class SqlHelperFactory {

    private static Map<String, ISqlHelper> helperMap = new HashMap<>();

    @Autowired(required = false)
    MysqlHelper mysqlHelper;

    @Autowired(required = false)
    SqlServerHelper sqlServerHelper;

    @Autowired(required = false)
    SqLiteHelper sqLiteHelper;

    @Autowired(required = false)
    PostgresqlHelper postgresqlHelper;

    @Autowired(required = false)
    OracleHelper oracleHelper;

    @Autowired(required = false)
    DB2Helper db2Helper;

    @Autowired(required = false)
    SybaseHelper sybaseHelper;

    public ISqlHelper getInstance(DSInfo dsInfo) {
        if(StringUtil.isEmpty(dsInfo.getDriverClassName())) {
            return null;
        }
        if(!helperMap.containsKey(dsInfo.getDriverClassName())) {
            synchronized (helperMap) {
                switch (dsInfo.getDriverClassName()) {
                    case "org.mariadb.jdbc.Driver":
                    case "com.mysql.cj.jdbc.Driver":
                    case "org.apache.derby.jdbc.ClientDriver":
                    case "com.mysql.jdbc.Driver":
                        if(mysqlHelper == null) {
                            mysqlHelper = new MysqlHelper() {};
                        }
                        helperMap.put(dsInfo.driverClassName, mysqlHelper);
                        break;
                    case "org.sqlite.JDBC":
                        if(sqLiteHelper == null) {
                            sqLiteHelper = new SqLiteHelper() {};
                        }
                        helperMap.put(dsInfo.driverClassName, sqLiteHelper);
                        break;
                    case "com.microsoft.sqlserver.jdbc.SQLServerDriver":
                        if(sqlServerHelper == null) {
                            sqlServerHelper = new SqlServerHelper() {};
                        }
                        helperMap.put(dsInfo.driverClassName, sqlServerHelper);
                        break;
                    case "org.postgresql.Driver":
                        if(postgresqlHelper == null) {
                            postgresqlHelper = new PostgresqlHelper() {};
                        }
                        helperMap.put(dsInfo.driverClassName, postgresqlHelper);
                        break;
                    case "oracle.jdbc.OracleDriver":
                        if(oracleHelper == null) {
                            oracleHelper = new OracleHelper() {};
                        }
                        helperMap.put(dsInfo.driverClassName, oracleHelper);
                        break;
                    case "driverManager:com.ibm.db2.jcc.DB2Driver":
                        if(db2Helper == null) {
                            db2Helper = new DB2Helper() {};
                        }
                        helperMap.put(dsInfo.driverClassName, db2Helper);
                        break;
                    case "com.sybase.jdbc2.jdbc.SybDriver":
                        if(sybaseHelper == null) {
                            sybaseHelper = new SybaseHelper() {};
                        }
                        helperMap.put(dsInfo.driverClassName, sybaseHelper);
                        break;
                }
            }
        }
        if(!helperMap.containsKey(dsInfo.driverClassName)) {
            return null;
        }
        return helperMap.get(dsInfo.driverClassName);
    }
}
